<?php


namespace app\admin\controller\v1;

use app\admin\model\MenuModel;
use app\admin\service\MenuService;
use app\admin\Validate\menu\AddMenuValidate;
use app\admin\Validate\menu\MenuListByPageValidate;
use app\admin\Validate\menu\UpdateMenuStatusByIdValidate;
use app\admin\Validate\menu\UpdateMenuValidate;
use app\BaseController;
use app\common\Token;
use think\Collection;

/**
 * 后台管理菜单
 * Class Menu
 * @package app\admin\controller\v1
 */
class Menu extends BaseController
{
    /**
     * 添加栏目
     * @return \think\response\Json
     */
    public function addMenu()
    {
        Token::checkToken($this->request);
        $param = (new AddMenuValidate)->validateParam();
        $menuModel = new MenuModel();
        $re = $menuModel->save($param);
        if ($re) {
            return successJson();
        }
        return errorJson(60001, "添加失败");
    }

    /**
     * 获取所有栏目
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getMenu()
    {
        Token::checkToken($this->request);
        $model = new MenuModel();
        $arr = $model->where('status', '=', 1)
            ->select()
            ->toArray();
        $re = (new MenuService)->categoryMenuByParentId($arr);
        return successJson($re);
    }

    /**
     * 获取menu列表
     * @return \think\response\Json
     * @throws \think\db\exception\DbException
     */
    public function getMenuList()
    {
        Token::checkToken($this->request);
        $param = (new MenuListByPageValidate)->validateParam();
        $model = new MenuModel();
        $data = $model->searchMenu($param);
        return successJson(
            $data, 'success'
        );
    }


    /**
     * 删除栏目
     * @param int $id 栏目id
     * @return \think\response\Json
     */
    public function deleteMenu(int $id)
    {
        Token::checkToken($this->request);
        $bool = MenuModel::destroy($id);
        if ($bool) {
            return successJson('success', "操作成功");
        }
        return errorJson(60002, "操作失败");
    }

    /**
     * 更新栏目
     * @return \think\response\Json
     */
    public function updateMenu()
    {
        Token::checkToken($this->request);
        $param = (new UpdateMenuValidate())->validateParam();
        $bool = MenuModel::update($param);
        if ($bool) {
            return successJson('success', "操作成功");
        }
        return errorJson(60002, "操作失败");
    }


    /**
     * 根据ID更新状态
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function updateMenuStatusById() {
        Token::checkToken($this->request);
        $param = (new UpdateMenuStatusByIdValidate())->validateParam();
        $bool = (new MenuModel())->updateStatusById($param['id'], $param['status']);
        if ($bool) {
            return successJson('success', "操作成功");
        }
        return errorJson(60002, "操作失败");
    }
}